package com.tg.data.http.entity;


import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.tg.appcommon.android.AlarmUtil;
import com.tange.base.toolkit.GlobalApplicationContext;
import com.tg.appcommon.android.Packet;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGLog;

public class AlarmToneCapBean implements Parcelable {
   public static final String ALARM_SAMPLE = "device_samplePerSecs";
   public static final String ALARM_CHANNEL = "device_mChannels";
   public static final String ALARM_BITPERSAMPLE = "device_mBitsPerSample";
   public static final String ALARM_AUDIOCODECS = "device_audioCodecs";
   public static final String ALARM_FILEFORMAT = "device_fileFormats";
   public static final String ALARM_SUPPORT_AUDIO = "device_nSupportedAudioCodecs";
   private boolean mFromLamp = false;
   public int mSamplePerSecs = 8000;
   public int mChannels = 1;
   public int mBitsPerSample = 16;
   public int audioCodecs = 2;
   public int fileFormats = 0;
   public int nSupportedAudioCodecs = 2;
   public boolean getDataSuccess = false;


   public StringBuilder strFormats;
   public StringBuilder strCodecs;
   private String uuid;
   public AlarmToneCapBean(){

   }
   public AlarmToneCapBean(String uuid, boolean fromLamp){
      this.uuid = uuid;
      this.mFromLamp = fromLamp;
      mSamplePerSecs = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_SAMPLE + uuid, 8000);
      mChannels = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_CHANNEL + uuid, 1);
      mBitsPerSample = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_BITPERSAMPLE + uuid, 16);
      audioCodecs = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_AUDIOCODECS + uuid, this.mFromLamp? AlarmUtil.TCMEDIA_AUDIO_PCM : AlarmUtil.TCMEDIA_AUDIO_G711A);
      fileFormats = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_FILEFORMAT + uuid, 0);
      nSupportedAudioCodecs = PreferenceUtil.getInt(GlobalApplicationContext.application(), ALARM_SUPPORT_AUDIO + uuid, 2);
   }

   public void save(){
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_SAMPLE + uuid, mSamplePerSecs);
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_CHANNEL + uuid, mChannels);
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_BITPERSAMPLE + uuid, mBitsPerSample);
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_AUDIOCODECS + uuid, audioCodecs);
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_FILEFORMAT + uuid, fileFormats);
      PreferenceUtil.setInt(GlobalApplicationContext.application(), ALARM_SUPPORT_AUDIO + uuid, nSupportedAudioCodecs);
   }

   protected AlarmToneCapBean(Parcel in) {
      mSamplePerSecs = in.readInt();
      mChannels = in.readInt();
      mBitsPerSample = in.readInt();
      audioCodecs = in.readInt();
      fileFormats = in.readInt();
      nSupportedAudioCodecs = in.readInt();
      getDataSuccess = in.readByte() != 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeInt(mSamplePerSecs);
      dest.writeInt(mChannels);
      dest.writeInt(mBitsPerSample);
      dest.writeInt(audioCodecs);
      dest.writeInt(fileFormats);
      dest.writeInt(nSupportedAudioCodecs);
      dest.writeByte((byte) (getDataSuccess ? 1 : 0));
   }

   @Override
   public int describeContents() {
      return 0;
   }

   public static final Creator<AlarmToneCapBean> CREATOR = new Creator<AlarmToneCapBean>() {
      @Override
      public AlarmToneCapBean createFromParcel(Parcel in) {
         return new AlarmToneCapBean(in);
      }

      @Override
      public AlarmToneCapBean[] newArray(int size) {
         return new AlarmToneCapBean[size];
      }
   };

   public void receiveIOCtrlData(@NonNull byte[] data) {
      TGLog.d("TCI_CMD_GET_ALARMTONE_CAP_RESP");
      getDataSuccess = true;
      mSamplePerSecs = Packet.byteArrayToShort_Little(data, 0);
      mBitsPerSample = data[2] & 0xFF;
      mChannels = data[3] & 0xFF;
      int nExpectedFileFormats = Packet.byteArrayToInt_Little(data, 4);
      byte[] expectedFileFormats = new byte[8];
      fileFormats = 0xff & expectedFileFormats[0];
      System.arraycopy(data, 8, expectedFileFormats, 0, expectedFileFormats.length);

      strFormats = new StringBuilder();
      String separator = ";";
      for (int i = 0; i < nExpectedFileFormats; i++) {
         String f = AlarmUtil.getFileFormat(expectedFileFormats[i]);
         if (!TextUtils.isEmpty(f)) {
            if (i > 0) {
               strFormats.append(separator);
            }
            strFormats.append(f);
         }
      }
      this.nSupportedAudioCodecs = Packet.byteArrayToInt_Little(data, 16);
      byte[] supportedAudioCodecs = new byte[8];
      System.arraycopy(data, 20, supportedAudioCodecs, 0, supportedAudioCodecs.length);
      audioCodecs = 0xff & supportedAudioCodecs[0];
      this.strCodecs = new StringBuilder();
      for (int i = 0; i < this.nSupportedAudioCodecs; i++) {
         int codes  = supportedAudioCodecs[i];
         String ac = AlarmUtil.getAudioCodecs(codes);
         if (codes == AlarmUtil.TCMEDIA_AUDIO_G711A ){
            audioCodecs = 0xff & codes;
         }
         if (!TextUtils.isEmpty(ac)) {
            if (i > 0) {
               this.strCodecs.append(separator);
            }
            this.strCodecs.append(ac);
         }
      }

      if (this.mFromLamp){
         audioCodecs = AlarmUtil.TCMEDIA_AUDIO_PCM;
      }
      TGLog.d("nSamplePerSecs " + mSamplePerSecs + " mBitsPerSample " + mBitsPerSample + " nChannels " + mChannels + " expectedFileFormats: " + strFormats + " " +
              " nExpectedFileFormats " + nExpectedFileFormats + " nSupportedAudioCodecs " + this.nSupportedAudioCodecs + " supportedAudioCodecs：" + strCodecs);

      save();
   }
}
